package com.web.netalbum.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.web.netalbum.bean.Album;
import com.web.netalbum.dao.AlbumDao;
import com.web.netalbum.util.DBUtil;

public class AlbumDaoImpl implements AlbumDao {
	private QueryRunner qr = new QueryRunner(DBUtil.getDataSource());

	@Override
	public Album findAlbumById(String id) {

		try {
			return qr.query("select * from album where id= ?",
					new BeanHandler<Album>(Album.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Album> findAllAlbum(String uid) {

		try {
			return qr.query("select * from album where uid=?",
					new BeanListHandler<Album>(Album.class), uid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public void updateAlbum(String name,String description,int id) {
		// TODO Auto-generated method stub
		try {
			qr.update(
					"update album set name=?,descritption=? where id=?",
					name, description, id);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	@Override
	public void deleteAlbum(String id) {

		try {
			qr.update("delete from album where id=?", id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void saveAlbum(Album album) {

		try {
			qr.update(
					"insert into album (name,descrition,open,contentTime,uid) values(?,?,?,?,?)",
					album.getName(), album.getDescription(), album.isOpen(),
					album.getContentTime(), album.getUid());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<Album> findAllAlbum() {
		try {
			return qr.query("select *from album", new BeanListHandler<Album>(Album.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Album findAlbumByName(String albumName) {
		try {
			return qr.query("select *from album where name=?", new BeanHandler<Album>(Album.class), albumName);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
